package model

import (
	"errors"
	"fmt"
	"go-zero-service/common/tool"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type User struct {
	ID         uint   `gorm:"primaryKey"`
	Username   string `gorm:"type:varchar(50);comment:用户名"`
	Mobile     string `gorm:"index:mobile;type:varchar(20)";comment:手机号`
	Password   string `gorm:"type:varchar(80)";comment:密码`
	Uuid       string `gorm:"type:varchar(50)";comment:uuid`
	Gender     int64  `gorm:"type:tinyint(50)";default:0;comment:性别`
	CreateTime int64  `gorm:"autoCreateTime;type:int(11)"`
	UpdateTime int64  `gorm:"autoUpdateTime;type:int(11)"`
	DeleteTime int64  `gorm:"type:int(11)"`
}

// 在创建前检验验证一下密码的有效性
func (u *User) BeforeCreate(db *gorm.DB) error {
	if len(u.Password) < 6 {
		return errors.New("密码太简单了")
	}
	//对密码进行加密存储
	u.Password = tool.Password(u.Password)
	logx.Info(fmt.Sprintf("密码加密后是：%s", u.Password))
	return nil
}
